package com.neuedu.boot.service.impl;

import com.neuedu.boot.entity.Department;
import com.neuedu.boot.entity.DepartmentExample;
import com.neuedu.boot.mapper.DepartmentMapper;
import com.neuedu.boot.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目：      springboot
 * 类名:       DeptService
 * 创建时间：  2024/3/29 14：41
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@Service
public class DeptService implements IDeptService {

    @Autowired
    DepartmentMapper departmentMapper;


    @Override
    public boolean saveOrupdate(Department dept) {
        if(dept.getId()!= null){
            return departmentMapper.updateByPrimaryKeySelective(dept)>0;
        }else{
            return departmentMapper.insertSelective(dept)>0;
        }

    }

    @Override
    public boolean updateByPk(Department dept) {
        return departmentMapper.updateByPrimaryKeySelective(dept)>0;
    }

    @Override
    public boolean removeById(int id) {
        return departmentMapper.deleteByPrimaryKey(id)>0;

    }

    @Override
    public List<Department> list(Department dept) {

        //select * from department
        DepartmentExample example = new DepartmentExample();
        if(dept.getName()!= null && !"".equals(dept.getName())){
            example.createCriteria().andNameLike("%"+dept.getName()+"%");  // where name like ?
        }
        return departmentMapper.selectByExample(example);
    }

    @Override
    public Department getById(int id) {
        return departmentMapper.selectByPrimaryKey(id);
    }
}
